<template>
  <header class="nav-header" :class="{ 'nav-scrolled': isScrolled }">
    <div class="logo">
      <router-link to="/"></router-link>
    </div>
    <div class="menu-toggle" @click="toggleMenu">
      <div class="hamburger" :class="{ 'active': isMenuOpen }">
        <span></span><span></span><span></span>
      </div>
    </div>
    <nav :class="{ 'active': isMenuOpen }">
      <router-link 
        v-for="item in menuItems" 
        :key="item.path" 
        :to="item.path"
        @click="closeMenu"
      >
        {{ item.name }}
      </router-link>
    </nav>
  </header>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useWindowSize } from '@vueuse/core'

const isScrolled = ref(false)
const isMenuOpen = ref(false)
const isMobile = ref(false)
const { width } = useWindowSize()

const menuItems = [
  { name: '首页', path: '/' },
  { name: '关于我', path: '/about' },
  { name: '项目', path: '/projects' },
  { name: '简历', path: '/resume' },
  { name: '代码', path: '/code-snippets' },
  { name: '联系', path: '/contact' }
]

const checkScroll = () => {
  isScrolled.value = window.scrollY > 50
}

const checkMobile = () => {
  isMobile.value = width.value < 768
}

const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value
  if (isMenuOpen.value) {
    document.body.style.overflow = 'hidden'
  } else {
    document.body.style.overflow = ''
  }
}

const closeMenu = () => {
  isMenuOpen.value = false
  document.body.style.overflow = ''
}

onMounted(() => {
  window.addEventListener('scroll', checkScroll)
  checkMobile()
  window.addEventListener('resize', checkMobile)
})

onUnmounted(() => {
  window.removeEventListener('scroll', checkScroll)
  window.removeEventListener('resize', checkMobile)
  document.body.style.overflow = ''
})
</script>

<style lang="scss" scoped>
.nav-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  padding: 0 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 1000;
  background: var(--background-color);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

  @supports (backdrop-filter: blur(10px)) {
    background: rgba(var(--background-color-rgb), 0.7);
    backdrop-filter: blur(15px);
  }

  nav {
    display: flex;
    gap: 2rem;

    a {
      color: var(--text-color);
      text-decoration: none;
      font-size: 1.1rem;
      transition: color 0.3s ease;
      position: relative;
      opacity: 1.0;

      &:hover {
        color: var(--primary-color);
        opacity: 1;
      }

      &.router-link-active {
        color: var(--primary-color);
        opacity: 1;
      }
    }
  }
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  z-index: 1001;
  
  a {
    color: var(--primary-color);
    text-decoration: none;
  }
}

.menu-toggle {
  display: none;
  z-index: 1001;
}

.hamburger {
  width: 30px;
  height: 20px;
  position: relative;
  cursor: pointer;
  
  span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text-color);
    position: absolute;
    transition: 0.3s;
    
    &:nth-child(1) { top: 0; }
    &:nth-child(2) { top: 9px; }
    &:nth-child(3) { top: 18px; }
  }
  
  &.active span {
    &:nth-child(1) {
      transform: rotate(45deg);
      top: 9px;
    }
    &:nth-child(2) {
      opacity: 0;
    }
    &:nth-child(3) {
      transform: rotate(-45deg);
      top: 9px;
    }
  }
}

@media (max-width: 768px) {
  .nav-header {
    padding: 0 1rem;
    background: rgba(var(--background-color-rgb), 0.9);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);

    @supports (backdrop-filter: blur(10px)) {
      background: rgba(var(--background-color-rgb), 0.6);
      backdrop-filter: blur(15px);
    }
  }

  .menu-toggle {
    display: block;
  }
  
  nav {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 70px 1rem 2rem;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 0.5rem;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    background: var(--background-color);

    &.active {
      transform: translateX(0);
    }

    @supports (backdrop-filter: blur(10px)) {
      background: rgba(var(--background-color-rgb), 0.95);
      backdrop-filter: blur(20px);
    }

    a {
      font-size: 1.2rem;
      width: 100%;
      text-align: center;
      padding: 1rem 1.5rem;
      border-radius: 8px;
      background: rgba(var(--background-color-rgb), 1.0);
      border: 1px solid rgba(var(--primary-color-rgb), 0.2);
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
      margin: 0.2rem 0;
      transition: all 0.3s ease;

      &:hover {
        background: rgba(var(--background-color-rgb), 1.0);
        transform: translateX(5px);
        border-color: rgba(var(--primary-color-rgb), 0.3);
      }
      
      &.router-link-active {
        background: rgba(var(--primary-color-rgb), 0.2);
        color: var(--primary-color);
        border-width: 2px;
        border-color: var(--primary-color);
        font-weight: 500;
        box-shadow: 0 2px 12px rgba(var(--primary-color-rgb), 0.15);
      }
    }
  }
}
</style> 